package com.yangrui.app.mapper;

import com.yangrui.app.entity.User;
import com.yangrui.app.entity.VideoOrder;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VideoOrderMapper {
    /**
     * 插入数据
     * @param videoOrder 实体
     * @return 1
     */
    @Insert("insert into lesson.video_order(order_no,state,total_fee,video_id,video_img,user_id)" +
            "values(#{orderNo},#{state},#{totalFee},#{videoId},#{videoImg},#{userId})")
    int insert(VideoOrder videoOrder);

    /**
     * 删除数据
     * @param id id
     * @return 1
     */
    @Delete("delete from lesson.video_order where id = #{param1}")
    int deleteById(Integer id);

    /**
     *  根据userid videoid 查询
     * @param userId userid
     * @param videoId videoid
     * @return VideoOrder
     */
    @Select("select id, order_no, state, total_fee, video_id, video_title, video_img, user_id, create_time" +
            " from lesson.video_order where user_id=#{param1} and video_id=#{param2}")
    VideoOrder selectByUserIdAndVideoId(Integer userId , Integer videoId);

    /**
     *  根据userId 查询VideoOrder集合
     * @param userId User
     * @return videoOrder
     */
    @Select("select id, order_no, state, total_fee, video_id, video_title, video_img, user_id, create_time" +
            " from lesson.video_order where user_id=#{param1}")
    List<VideoOrder> selectByUserId(Integer userId);

    @Select("select id, order_no, state, total_fee, video_id, video_title, video_img, user_id, create_time" +
            " from lesson.video_order where id=#{param1}")
    @Results({
            @Result(id = true,column = "id",property = "id" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "user_id",property = "user",javaType = User.class,
            one = @One(select = "com.yangrui.app.mapper.UserMapper.selectById"))
    }
    )
    VideoOrder selectDetailById(Integer id);
}
